package com.zust.zst.chooselesson.service.impl;

import com.zust.zst.chooselesson.service.CultivationSystemService;
import com.zust.zst.common.dto.chooselesson.AddCultivationPlanDTO;
import com.zust.zst.common.dto.chooselesson.ColGraMajDTO;
import com.zust.zst.common.vo.chooselesson.CultivationPlanVO;
import com.zust.zst.dao.mapper.chooselesson.CourseInfoManagementMapper;
import com.zust.zst.dao.mapper.chooselesson.CultivationSystemMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 培养计划接口实现类
 *
 * @author Gavin Zhang
 * @date 2022/12/7 16:22
 */

@Service("cultivationSystemServiceImpl")
public class CultivationSystemServiceImpl implements CultivationSystemService {

    @Resource
    private CultivationSystemMapper cultivationSystemMapper;

    @Resource
    private CourseInfoManagementMapper courseInfoManagementMapper;

    @Override
    public List<Integer> getAllGradeFromCollege(String college) {
        if (StringUtils.isNoneBlank(college)) {

            // 获取学院id
            int id = courseInfoManagementMapper.getCollegeIdByName(college);

            return cultivationSystemMapper.getAllGradeByCollegeId(id);
        }
        return null;
    }

    @Override
    public List<String> getMajorByCollegeAndGrade(String college, Integer grade) {
        if (StringUtils.isNoneBlank(college) && grade != null) {
            // 获取学院id
            int id = courseInfoManagementMapper.getCollegeIdByName(college);
            return cultivationSystemMapper.getMajorByCollegeAndGrade(id, grade);
        }

        return null;
    }

    @Override
    public List<CultivationPlanVO> getCultivationByCollegeAndGradeAndMajor(ColGraMajDTO colGraMajDTO) {

        // 获取学院id
        int id = cultivationSystemMapper.getMajorIdByColGraMaj(colGraMajDTO.getGrade(), colGraMajDTO.getMajor());
        int page = (colGraMajDTO.getPage() - 1) * colGraMajDTO.getPageSize();
        colGraMajDTO.setPage(page);
        return cultivationSystemMapper.getCultivationByMajor(id, colGraMajDTO);
    }

    @Override
    public Integer getTotalByCultivationPlan(ColGraMajDTO colGraMajDTO) {

        // 获取学院id
        int majorId = cultivationSystemMapper.getMajorIdByColGraMaj(colGraMajDTO.getGrade(), colGraMajDTO.getMajor());
        return cultivationSystemMapper.getCultivationCountByMajor(majorId);
    }

    @Override
    public int addCultivationPlanCourse(AddCultivationPlanDTO dto) {

        // 获取majorId
        int majorId = cultivationSystemMapper.getMajorIdByColGraMaj(dto.getGrade(), dto.getMajor());

        return cultivationSystemMapper.addCultivationPlanCourse(majorId, dto);
    }
}
